<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\api\controller;
use \think\Controller;

class Goods extends Controller{

  public function _initialize(){
      header("Access-Control-Allow-Origin: * ");
      header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
      header('Access-Control-Allow-Headers:x-requested-with,content-type');

  }

	public function getGoodscat(){
    $cats = model('Goodscat')->where(['status' => 1])->order('sort asc')->select();
    //默认商品数据
    foreach ($cats as $key => $value) {
        $definedInfo = $this->getGoodsBycid($cats[$key]['id']);
        $cats[$key]['goodslist'] = $definedInfo['list'];
        $cats[$key]['page'] = $definedInfo['page'];
        $cats[$key]['end'] = $definedInfo['end'];
    }
    return $cats;
	}

  public function getGoodsBycid($cid = null, $page = 1){
    if($cid){
      $list = model('goods')->with('goodsrule')->where(['cat_id'=>$cid])->order('sort asc,update_time desc,id desc')->limit(10)->page($page)->select();
      $data['end'] = 0;
      if(model('goods')->where(['cat_id'=>$cid])->limit(10)->page($page)->count() < 10){
        $data['end'] = 1;
      }
      $data['list'] = $list;
      $data['page'] = $page+ 1;
      return $data;
    }
  }

  public function refresh(){
    $id = input('post.id');

    if(!$id){
      $this->error('参数错误');
    }
    $result =  db('Goods')->where('id', $id)->update(['update_time'=>time(),'sort'=>0]);
    if($result){
      $this->success('刷新成功');
    }else{
      $this->success('刷新失败');
    }

  }

  public function setStatus(){
    $id = input('post.id');
    $status = input('post.status');
    if(!$id){
      $this->error('参数错误');
    }
    $result =  db('Goods')->where('id', $id)->update(['status'=>$status]);
    if($result){
      $this->success('操作成功');
    }else{
      $this->error('操作失败');
    }
  }
  public function setStock(){
    $id = input('post.id');
    $stock = (int)input('post.stock');
    if(!$id){
      $this->error('参数错误');
    }
    if($stock < 0){
      $this->error('库存不能小于0');
    }
    $result =  db('Goodsrule')->where('id', $id)->update(['stock'=>$stock]);
      trace($stock,'error');
    if($result){
      $this->success('操作成功');
    }else{
      $this->error('操作失败');
    }
  }
  public function getToken(){
    $corpid = 'wwe8857dbaeeda1ec4';
    $corpsecret = 'la0ObOTvrQhsTIA_3vchvO7X-P_XawZp0wamfYPEV0Q';
    $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$corpsecret}";
    $getInfo = http_get($url);
    $info = json_decode($getInfo, true);
    $token = $info['access_token'];
    return $token;
  }

  public function getUserInfo(){
      $code = input('code');

      $token = $this->getToken();
      $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token}&code={$code}";
      $getInfo = http_get($url);
      $info = json_decode($getInfo, true);
      \think\Log::record($info,'error');
      return $info;
  }


}
